using UnityEngine;

public class RandomYRotator : MonoBehaviour
{
    public GameObject[] targets; // 拖入3个对象
    public float rotationSpeed = 30f; // 每秒旋转度数

    private GameObject currentTarget;
    private int lastIndex = -1;

    void Start()
    {
        ActivateRandomTarget();
    }

    void Update()
    {
        // 每帧插值旋转当前目标
        if (currentTarget != null)
        {
            Quaternion currentRot = currentTarget.transform.rotation;
            Quaternion targetRot = currentRot * Quaternion.Euler(0, rotationSpeed * Time.deltaTime, 0);
            currentTarget.transform.rotation = Quaternion.Slerp(currentRot, targetRot, 0.1f);
        }

        // 按空格手动触发切换
        if (Input.GetKeyDown(KeyCode.Space))
        {
            ActivateRandomTarget();
        }
    }

    public void ActivateRandomTarget()
    {
        // 全部禁用
        foreach (var obj in targets)
        {
            obj.SetActive(false);
        }

        // 随机选一个新的 index（不能和上次一样）
        int index;
        do
        {
            index = Random.Range(0, targets.Length);
        } while (index == lastIndex && targets.Length > 1);

        // 启用新的目标
        lastIndex = index;
        currentTarget = targets[index];
        currentTarget.SetActive(true);
    }
}
